<?php

namespace app\api\controller;

use app\admin\model\Captcha;
use app\admin\model\Members;
use app\home\validate\Reg;
use Firebase\JWT\JWT;
use think\Config;
use think\Controller;
use think\Db;
use think\Request;

/**
 * 注册登陆
 */
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization");
header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE,OPTIONS,PATCH');
class Index extends Controller
{

    /**
     * 登陆
     */
    public function login(Request $request){

        $mobile      = $request->post('mobile');
        $password    = $request->post('password');
        $validate    = validate('Reg');
        $v_data      = $request->post();
        if(!$validate->scene('login')->check($v_data)){
            json_return(201,$validate->getError());
        }
        $User      = Db::name('member');
        $user_info =$User
                ->field('id,nickname,head,money,password,sex,sign,create_at,is_say,is_room')
                ->where(array('mobile'=>$mobile))
                ->find();
        if(!$user_info){
            json_return(201,'用户不存在');
        }
        if(data_md5_key($password)!=$user_info['password']){
            json_return(201,'用户名密码错误');
        }
        unset($user_info['password']);
        $this->do_login_log($user_info['id']);
        $data['token'] = md5(rand(1111,9999));
        $User->where(array('mobile'=>$mobile))->update($data);
        $user_info['token'] = $data['token'];
        $RongYun   = new Rongyun();
        $token     =  $RongYun->get_token($user_info['id'],$user_info['nickname'],$user_info['head']);
        $user_info['r_token'] = $token;
        $Keys = new \live\WebRTCSigApi();
        $user_sig = $Keys->get_my_user_sig($user_info['id']);
        $user_info['user_sig'] = $user_sig;
        json_return(200,'登录成功',$user_info);


    }

    /**
     * 用户注册
     */
    public function register(Request $request){

        $mobile     = $request->post('mobile');
        $code       = $request->post('code');
        $v_data     = $request->post();
        $validate   = validate('Reg');
        if(!$validate->scene('forgets')->check($v_data)){
            json_return(201,$validate->getError());
        }
        if(!$v_data['nickname']){
            json_return(201,'昵称不能为空');
        }
        $Captcha      = new Captcha();
        $captcha_info = $Captcha->where(['mobile'=>$mobile])->find();
        $true_code    = $captcha_info->code;
        if($true_code!=$code){
            json_return(201,'验证码错误');
        }
        $User      = new Members();
        $user_info = $User->where(array('mobile'=>$mobile))->find();
        if($user_info){
            json_return(201,'账号已经注册');
        }
        $result0 = $User->register_add($v_data);
        if ($result0) {
            json_return(200,'注册成功');
        } else {
            json_return(201,'注册失败');
        }
    }

    /**
     * 找回密码
     */
    public function forget_password(Request $request){

        $mobile     = $request->post('mobile');
        $code       = $request->post('code');
        $password   = $request->post('password');
        $v_data     = $request->post();
        $validate   = validate('Reg');
        if(!$validate->scene('forgets')->check($v_data)){
            json_return(201,$validate->getError());
        }
        $Captcha      = new Captcha();
        $captcha_info = $Captcha->where(array('mobile'=>$mobile))->find();
        $true_code    = $captcha_info['code'];
        if($true_code!= $code){
            json_return(201,'验证码错误');
        }
        $up['password'] = data_md5_key($password);
        $result = Db::name('member')->where('mobile',$mobile)->update($up);
        if ($result) {
            json_return(200,'修改密码成功');
        } else {
            json_return(201,'修改密码失败');
        }
    }

    /**

     *  发送短信验证码

     * */
    public function send_message(Request $request){
        $v_data     = $request->post();
        $mobile     = $request->post('mobile');
        $validate   = validate('Reg');
        if(!$validate->scene('send')->check($v_data)){
            json_return(201,$validate->getError());
        }
        if(!preg_match("/^1[34578]\d{9}$/", $mobile)){
            json_return(201,'手机号码格式有误');
        }
        $mobile_code = rand(1111,9999);
        //短信接口地址
        $content = urlencode('#code#='.$mobile_code);
        $url     = "http://v.juhe.cn/sms/send?mobile=".$mobile."&tpl_id=89034"."&tpl_value=".$content."&key=b78af0b4ca07dfecbf5cdcae8fd6fc6f";
        $gets    =  httpGet($url);
        $gets    = json_decode($gets,true);
        $Captcha = Db::name('captcha');
        $captcha_info = $Captcha->where(array('mobile'=>$mobile))->find();
        if($captcha_info['number']>10){
            json_return(201,'今日发短信超过规定次数');
        }
        if($gets['error_code']==0){
            if($captcha_info){
                if($captcha_info['create_at']<strtotime(date('Y-m-d'))){
                    $Captcha->where(array('mobile'=>$mobile))->update(['number'=>0]);
                }
                $save_data['code']      = $mobile_code;
                $save_data['create_at'] = time();
                $save_data['number']    = array("exp", "number+" . 1);
                $Captcha->where(array('mobile'=>$mobile))->update($save_data);
            }else{
                $add_data['code'] = $mobile_code;
                $add_data['create_at'] = time();
                $add_data['number'] = 1;
                $add_data['mobile'] = $mobile;
                $Captcha->insert($add_data);
            }
            json_return(200,'发送成功');
        }else{
            json_return(201,'发送失败');
        }
    }

    /**
     * 版本控制
     */
    public function get_version(Request $request){
        $app_id       = $request->post('app_id');
        $version_code = $request->post('version_code');
        if(!$app_id || !$version_code){
            json_return(204, '缺少参数');
        }
        $condition  = array(
            'app_id'=>$app_id,
        );
        $condition0 = array(
            'app_id'=>$app_id,
            'version_code'=>$version_code
        );
        $version_info0 = Db::name('version')
            ->where($condition0)
            ->find();
        $version_info = Db::name('version')
            ->where($condition)
            ->order('id desc')
            ->find();
        if($version_info0['version_code']==$version_info['version_code']){
            $return_data['is_new']    = 1;
        }else{
            $return_data['is_new']    = 0;
        }
        $return_data['version_info'] = $version_info;
        json_return(200, '成功',$return_data);
    }

    /**
     * 根据ip获取地址
     */
    public function do_login_log($uid){
        $login_log['uid']             = $uid;
        $login_log['login_way']       = is_ios();
        $login_log['create_at']       = time();
        $login_log['ip']              = \request()->ip();
        $map = [
            'ip'=>$login_log['ip']
        ];
        $ip_address = Db::name('login_log')
            ->where($map)
            ->value('ip_address');
        if(!$ip_address){
            $ip_address = get_city_id($login_log['ip']);
        }
        $login_log['ip_address']     = $ip_address ;
        Db::name('login_log')->insert($login_log);
    }
    
    public function test0828(){
        $Keys = new \live\WebRTCSigApi();

        $user_sig = $Keys->get_my_user_sig(1);
        var_dump($user_sig);exit;
    }




}